﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using MODEL;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 广告信息
    /// </summary>
    public class AdInformation
    {
        public AdInformation()
        { }
        /// <summary>
        /// 广告信息 添加
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool InformationAdd(MODEL.AdInformation model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                        new SqlParameter("@title",SqlDbType.VarChar,50),
                                        new SqlParameter("@url",SqlDbType.VarChar,200),
                                        new SqlParameter("@begintime",SqlDbType.DateTime),
                                        new SqlParameter("@endtime",SqlDbType.DateTime),
                                        new SqlParameter("@state",SqlDbType.Bit),
                                        new SqlParameter("@position",SqlDbType.VarChar,11),
                                        new SqlParameter("@imgurl",SqlDbType.VarChar,200),
                                        new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Atitle;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.Aurl;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.AbeginTime;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.AendTime;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.Astate;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.Aposition;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.Aimgurl;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdinformationAdd", parameter);
        }
        /// <summary>
        /// 广告信息 删除
        /// </summary>
        /// <param name="adId">广告编号</param>
        /// <returns>是否执行成功</returns>
        public bool InformationDel(string adId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = adId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdinformationDelete", parameter);
        }
        /// <summary>
        /// 广告信息 更新
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool InformationUpdate(MODEL.AdInformation model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                        new SqlParameter("@title",SqlDbType.VarChar,50),
                                        new SqlParameter("@url",SqlDbType.VarChar,200),
                                        new SqlParameter("@endtime",SqlDbType.DateTime),
                                        new SqlParameter("@state",SqlDbType.Bit),
                                        new SqlParameter("@position",SqlDbType.VarChar,11),
                                        new SqlParameter("@imgurl",SqlDbType.VarChar,200),
                                        new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Atitle;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.Aurl;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.AendTime;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.Astate;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.Aposition;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.Aimgurl;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdinformationUpdate", parameter);
        }
        /// <summary>
        /// 广告信息 修改广告状态
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>示范法执行成功</returns>
        public bool InformationUpdateState(MODEL.AdInformation model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@state",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Astate;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdinformationUpdateState", parameter);
        }
        /// <summary>
        /// 广告信息 列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet GetAdInformationList()
        {
            return DbHelper.RunProcedure("ProAdinformationSelectAll");
        }
        /// <summary>
        /// 广告信息 实体
        /// </summary>
        /// <param name="adId">广告编号</param>
        /// <returns>实体</returns>
        public MODEL.AdInformation GetAdInformationModel(string adId)
        {
            MODEL.AdInformation model = new MODEL.AdInformation();
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 11) };
            parameter[0].Value = adId;
            parameter[0].Direction = ParameterDirection.Input;
            DataSet ds = DbHelper.RunProcedureS("ProAdinformationSelectByID", parameter);
            model.AID = adId;
            model.Atitle = ds.Tables[0].Rows[0]["atitle"].ToString();
            model.Aurl = ds.Tables[0].Rows[0]["aurl"].ToString();
            model.AbeginTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["abegingtime"]);
            model.AendTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["aendtime"]);
            model.Astate = Convert.ToBoolean(ds.Tables[0].Rows[0]["astate"]);
            model.Aposition = ds.Tables[0].Rows[0]["aposition"].ToString();
            model.Aimgurl = ds.Tables[0].Rows[0]["Aimgurl"].ToString();
            return model;
        }
        /// <summary>
        ///  广告信息 最大编号
        /// </summary>
        /// <returns>最大编号</returns>
        public string GetMaxID()
        {
            string sqlStr = "SELECT MAX(AID) FROM G_ADINFORMATION";
            string maxId = string.Empty;
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "" || ds.Tables[0].Rows[0][0].ToString() == null)
            {
                maxId = DateTime.Now.ToString("yyyyMMdd") + "001";
            }
            else
            {
                int m = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(8, 3)) + 1;
                maxId = DateTime.Now.ToString("yyyyMMdd") + string.Format("{0:D3}", m);
            }
            return maxId;
        }
        /// <summary>
        /// 广告信息 可用广告位
        /// </summary>
        /// <returns>可用广告位</returns>
        public DataSet GetAdpositionList()
        {
            SqlParameter[] parameter = { new SqlParameter("@state", SqlDbType.Bit) };
            parameter[0].Value = true;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProAdpositionSelectByState", parameter);
        }

    }
    /// <summary>
    /// 广告位信息
    /// </summary>
    public class AdPosition
    {
        public AdPosition()
        { }
        /// <summary>
        /// 广告位 添加
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool PositionAdd(MODEL.AdPosition model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@title",SqlDbType.VarChar,50),
                                       new SqlParameter("@page",SqlDbType.VarChar,50),
                                       new SqlParameter("@width",SqlDbType.Int),
                                       new SqlParameter("@height",SqlDbType.Int),
                                       new SqlParameter("@state",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Atitle;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.Apage;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.Awidth;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.Aheight;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.Astate;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdpositionAdd", parameter);
        }
        /// <summary>
        /// 广告位 删除
        /// </summary>
        /// <param name="positionId">广告位编号</param>
        /// <returns>是否执行成功</returns>
        public bool PositionDel(string positionId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = positionId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdpositionDelete", parameter);
        }
        /// <summary>
        /// 广告位 更新
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool PositionUpdate(MODEL.AdPosition model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@title",SqlDbType.VarChar,50),
                                       new SqlParameter("@page",SqlDbType.VarChar,50),
                                       new SqlParameter("@width",SqlDbType.Int),
                                       new SqlParameter("@height",SqlDbType.Int),
                                       new SqlParameter("@state",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Atitle;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.Apage;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.Awidth;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.Aheight;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.Astate;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdpositionUpdate", parameter);
        }
        /// <summary>
        /// 广告位 可用状态修改
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool PositionUpdateState(MODEL.AdPosition model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,11),
                                       new SqlParameter("@state",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.AID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Astate;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProAdpositionUpdateState", parameter);
        }
        /// <summary>
        /// 广告位 列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet GetAdPositionList()
        {
            return DbHelper.RunProcedure("ProAdpositionSelectAll");
        }
        /// <summary>
        /// 广告位 列表
        /// </summary>
        /// <param name="adPage">所在页面</param>
        /// <returns>列表</returns>
        public DataSet GetAdPositionList(string adPage)
        {
            SqlParameter[] parameter = { new SqlParameter("@page", SqlDbType.VarChar, 50) };
            parameter[0].Value = adPage;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProAdpositionSelectByPage", parameter);
        }
        /// <summary>
        /// 广告位 实体
        /// </summary>
        /// <param name="adId">编号</param>
        /// <returns>实体</returns>
        public MODEL.AdPosition GetAdPositionModel(string adId)
        {
            MODEL.AdPosition model = new MODEL.AdPosition();
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 11) };
            parameter[0].Value = adId;
            parameter[0].Direction = ParameterDirection.Input;
            DataSet ds = DbHelper.RunProcedureS("ProAdpositionSelectByID", parameter);
            model.AID = adId;
            model.Atitle = ds.Tables[0].Rows[0]["atitle"].ToString();
            model.Apage = ds.Tables[0].Rows[0]["apage"].ToString();
            model.Awidth = Convert.ToInt32(ds.Tables[0].Rows[0]["awidth"]);
            model.Aheight = Convert.ToInt32(ds.Tables[0].Rows[0]["aheight"]);
            model.Astate = Convert.ToBoolean(ds.Tables[0].Rows[0]["astate"]);
            return model;
        }
        /// <summary>
        /// 广告位 最大编号
        /// </summary>
        /// <returns>最大编号</returns>
        public string GetMaxID()
        {
            string sqlStr = "SELECT MAX(AID) FROM G_ADPOSITION";
            string maxId=string.Empty;
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "" || ds.Tables[0].Rows[0][0].ToString() == null)
            {
                maxId = DateTime.Now.ToString("yyyyMMdd") + "001";
            }
            else
            {
                int m = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(8, 3)) + 1;
                maxId = DateTime.Now.ToString("yyyyMMdd") + string.Format("{0:D3}", m);
            }
            return maxId;
        }
    }
}
